﻿
using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;


namespace Production_Set.API.Application.Commands.Supplier
{
    /// <summary>
    /// 供应商修改
    /// </summary>
    public class SupplierUpdateHandler : IRequestHandler<SupplierUpdateCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly IBaseRepository<Suppliers> supplierRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="supplierRepository"></param>
        /// <param name="mapper"></param>
        public SupplierUpdateHandler(IBaseRepository<Suppliers> supplierRepository, IMapper mapper)
        {
            this.supplierRepository = supplierRepository;
            this.mapper = mapper;
        }


        public async Task<Result> Handle(SupplierUpdateCommand request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Update };

            try
            {
                var AddData = mapper.Map<Suppliers>(request);
                string sql = "UPDATE Suppliers SET SuppliersNo=@SuppliersNo ,SuppliersName=@SuppliersName ,SuppliersType=@SuppliersType ,SuppliersPhone=@SuppliersPhone,SuppliersAdder=@SuppliersAdder ,CountryId=@CountryId,CityId=@CityId,SupplierClassification =@SupplierClassification ,SupplierLabels =@SupplierLabels,CompanyLogo =@CompanyLogo,OutsideLabel=@OutsideLabel,SuppliersStatus =1 WHERE SuppliersId =@SuppliersId ";
                var AddNum = supplierRepository.Update(sql, new
                {
                    SuppliersId = AddData.SuppliersId,
                    SuppliersNo = AddData.SuppliersNo,
                    SuppliersName = AddData.SuppliersName,
                    SuppliersType = AddData.SuppliersType,
                    SuppliersPhone = AddData.SuppliersPhone,
                    SuppliersAdder = AddData.SuppliersAdder,
                    CountryId = AddData.CountryId,
                    CityId = request.CityId,
                    SupplierClassification = AddData.SupplierClassification,
                    SupplierLabels = AddData.SupplierLabels,
                    CompanyLogo = AddData.CompanyLogo,
                    OutsideLabel = AddData.OutsideLabel,
                    SuppliersStatus = AddData.SuppliersStatus,
                });
                if (AddNum > 0)
                {
                    results.Code = (int)CommonErrorCodes.Success;
                    return results;
                }
                else
                {
                    return results;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
